package com.jshoperxms;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.jshoperxms.components.wechat.WxMappingJackson2HttpMessageConverter;
import com.jshoperxms.controller.mall.backstage.init.InitDataListenter;
import com.jshoperxms.controller.utils.statickey.RedisKey;
import com.jshoperxms.entity.GlobalParamM;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.*;

import javax.persistence.EntityManagerFactory;
import java.util.Iterator;
import java.util.Map;

/**
 * @author Arvin.Cao
 * @version 1.0
 * @time 16/3/23 下午12:31
 * @Description SpringMVC配置
 */
//@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {

    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "classpath:/META-INF/resources/", "classpath:/resources/",
            "classpath:/static/", "classpath:/public/"};

    /**
     * 不拦截的路径
     */
    private final static String[] URL_EXCLUDE_PATTERNS = new String[] { "/mall/user"};

    @Bean
    public InitDataListenter initDataListenter(){
        return new InitDataListenter();
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
        super.addResourceHandlers(registry);
    }



    @Bean
    public HibernateJpaSessionFactoryBean sessionFactory() {
        return new HibernateJpaSessionFactoryBean();
    }


    /**
     * 添加JSON的序列化工具
     *
     * @return gson
     */
    @Bean
    public HttpMessageConverter jsonConverters() {
        HttpMessageConverter gson = new GsonHttpMessageConverter();
        return gson;
    }


    @Override
    protected void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController("/","/mosasaur/mosasaur.html");
        super.addViewControllers(registry);
    }

    @Bean
    RestTemplate restTemplate(){
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().add(new WxMappingJackson2HttpMessageConverter());
        return restTemplate;
    }

    @Override
    protected void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedHeaders("Access-Control-Allow-Origin", "*")
                .allowedMethods("Access-Control-Allow-Methods", "*")
                .allowedOrigins("*");
    }


}
